package com.keelau.ui.fx;

import com.keelau.config.dataconfig.MyBatisConfig;
import javafx.application.Application;
import javafx.application.Platform;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.layout.AnchorPane;
import javafx.stage.Stage;
import javafx.stage.WindowEvent;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

import java.util.concurrent.CompletableFuture;


public class mainWindow extends Application {

    Logger logger = LoggerFactory.getLogger(this.getClass());

    public  static  AnnotationConfigApplicationContext context;


    @Override
    public void init() throws Exception {
/*
        CompletableFuture.supplyAsync(() -> {
            AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(MyBatisConfig.class);
            return context;
        }).thenAccept(this::launchApplicationView);
*/
        context = new AnnotationConfigApplicationContext(MyBatisConfig.class);
    }

    private void launchApplicationView(AnnotationConfigApplicationContext applicationContext) {
        logger.info("init spring config");
        context = applicationContext;
    }



    @Override
    public void start(Stage primaryStage) throws Exception {
       // Parent root = FXMLLoader.load(getClass().getResource("AliProductMainFx.fxml"));// new AliProductMainFX();
        logger.info("start ui");
        AnchorPane root = new AnchorPane();

        mainLayoutUI mainLayoutUI = new mainLayoutUI();

        //mainLayoutUI.setBorder(ComponentUtils.getBorder(null));

        AliDetailCrawlerUI aliDetailCrawlerUI = new AliDetailCrawlerUI();

        mainLayoutUI.setRightMenu(aliDetailCrawlerUI);
        ComponentUtils.fillParent(mainLayoutUI);


        root.getChildren().add(mainLayoutUI);

        Scene scene = new Scene(root,900,900);

        primaryStage.setMaximized(true);
        primaryStage.setTitle("凌硕科技");
        primaryStage.setScene(scene);
        primaryStage.show();
        primaryStage.setOnCloseRequest(new EventHandler<WindowEvent>() {
            @Override
            public void handle(WindowEvent event) {

                Platform.exit();
                logger.info("stage closed");
            }
        });

    }

    @Override
    public void stop() throws Exception {
        super.stop();
        if(AliDetailCrawlerUI.browserList!=null && AliDetailCrawlerUI.browserList.size()>0) {
            AliDetailCrawlerUI.browserList.forEach(t -> {
                t.dispose();
            });
        }
    }

    public  static void main(String[] args){
        launch(args);
    }
}
